package io.github.wjehovah.yyn.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import io.github.wjehovah.yyn.R;

/**
 * 登录注册页面
 * Created by yingchen on 2015/8/5.
 */
public class LoginRegistActivity extends Activity implements View.OnClickListener{
    //后退按钮
    private ImageButton mImageButtonBack;

    //切换登录注册
    private TextView mTextViewSwitch;

    //包含电话框 密码框  按钮的父布局
    private RelativeLayout mRelativeLayout;

    //电话号码
    private EditText mEditTextNumber;

    //密码
    private EditText mEditTextPassword;


    //登录按钮
    private Button mButtonLogin;

    //注册按钮
    private Button mButtonRegist;

    //包含
    private FrameLayout mFrameLayout;

    //QQ登录
    private ImageButton mImageButtonQQLogin;

    //微博登录
    private ImageButton mImageButtonWBLogin;

    //腾讯微博登录
    private ImageButton mImageButtonTXWBLogin;

    //标识显示登陆的界面  默认为true;
    private boolean showLogin = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_regist_view);

        //初始化一些控件
        initWidegt();

        //新建按钮 动态往FrameLayout中添加
        createButtons();

        addListenerOnButton();

        //获取Intent中的标识  判断是登录还是注册
        String action = getIntent().getStringExtra("ACTION");
        if(!TextUtils.isEmpty(action)&&"login".equals(action)){
            //点击的是登陆按钮
            mTextViewSwitch.setText("注册账号");
            mEditTextNumber.setHint("手机号");

            mFrameLayout.removeAllViews();

            mFrameLayout.addView(mButtonLogin);

            //修改显示界面的标识
            showLogin = true;

        }else if(!TextUtils.isEmpty(action)&&"regist".equals(action)){
            //点击的是注册按钮
            mTextViewSwitch.setText("已有账号?");
            mEditTextNumber.setHint("请输入手机号");


            mFrameLayout.removeAllViews();

            mFrameLayout.addView(mButtonRegist);

            //修改显示界面的标识
            showLogin = false;
        }



        overridePendingTransition(R.anim.bottom_in, R.anim.top_out);

    }

    /**
     * 给按钮添加监听
     */
    public void addListenerOnButton(){
        //后退按钮
        mImageButtonBack.setOnClickListener(this);

        //切换登录 注册
        mTextViewSwitch.setOnClickListener(this);
    }

    /**
     * 初始化控件
     */
    public void initWidegt(){
        mImageButtonBack = (ImageButton) findViewById(R.id.ib_login_regist_back);
        mTextViewSwitch = (TextView) findViewById(R.id.tv_login_regist_swtich);
        mEditTextNumber = (EditText) findViewById(R.id.et_login_regist_number);
        mEditTextPassword = (EditText) findViewById(R.id.et_login_regist_password);
      /*  mButtonLogin = (Button) findViewById(R.id.btn_login_regist_login);
        mButtonRegist = (Button) findViewById(R.id.btn_login_regist_regist);*/
        mImageButtonQQLogin = (ImageButton) findViewById(R.id.ib_login_regist_qqlogin);
        mImageButtonWBLogin = (ImageButton) findViewById(R.id.ib_login_regist_wblogin);
        mImageButtonTXWBLogin = (ImageButton) findViewById(R.id.ib_login_regist_txwblogin);

        mFrameLayout = (FrameLayout) findViewById(R.id.fl_login_regist);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl_login_regist_container);
    }

    /**
     * 点击按钮的回调事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_login_regist_back: //后退按钮
                finish();
                break;
            case R.id.tv_login_regist_swtich: //切换登录注册
                switchLoginRegist();
                break;
            default:

                break;
        }
    }




    private  Animation left_out;
    private  Animation right_in;
    private  Animation right_out;
    private  Animation left_in;

    /**
     * 切换登录 注册
     */

    public void switchLoginRegist(){

        if(showLogin){ //登录--->注册
            mTextViewSwitch.setText("已有账号?");
            if(left_out==null){
                left_out = AnimationUtils.loadAnimation(LoginRegistActivity.this, R.anim.left_out);
            }
            left_out.setFillAfter(true);
            mRelativeLayout.startAnimation(left_out);
            left_out.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }
                @Override
                public void onAnimationEnd(Animation animation) {
                    mEditTextNumber.setHint("请输入手机号");
                    mEditTextPassword.setHint("请输入密码");

                    mFrameLayout.removeAllViews();
                    mFrameLayout.addView(mButtonRegist);
                    if(right_in == null){
                        right_in = AnimationUtils.loadAnimation(LoginRegistActivity.this, R.anim.right_in);
                    }
                    right_in.setFillAfter(true);
                    mRelativeLayout.startAnimation(right_in);

                    //改变标识显示登陆注册的标识
                    showLogin = !showLogin;
                }
                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });


        }else{  //注册--->登录

            mTextViewSwitch.setText("注册账号");

            if(right_out==null){
                right_out = AnimationUtils.loadAnimation(LoginRegistActivity.this, R.anim.right_out);
            }
            right_out.setFillAfter(true);
            mRelativeLayout.startAnimation(right_out);

            right_out.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    mEditTextNumber.setHint("手机号");
                    mEditTextPassword.setHint("密码");

                    mFrameLayout.removeAllViews();
                    mFrameLayout.addView(mButtonLogin);

                    if(left_in==null){
                        left_in = AnimationUtils.loadAnimation(LoginRegistActivity.this, R.anim.left_in);
                    }
                    left_in.setFillAfter(true);
                    mRelativeLayout.startAnimation(left_in);

                    //改变标识显示登陆注册的标识
                    showLogin = !showLogin;
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

        }

    }

    public void  createButtons(){
        //新建一个登陆按钮
        mButtonLogin = new Button(LoginRegistActivity.this);
        mButtonLogin.setBackgroundColor(Color.RED);
        mButtonLogin.setText("登录");
        mButtonLogin.setTextColor(Color.WHITE);
        mButtonLogin.setBackgroundResource(R.drawable.login_regist_buttonshape);

        //新建一个注册按钮
        mButtonRegist = new Button(LoginRegistActivity.this);
        mButtonRegist.setBackgroundColor(Color.RED);
        mButtonRegist.setText("注册");
        mButtonRegist.setTextColor(Color.WHITE);
        mButtonRegist.setBackgroundResource(R.drawable.login_regist_buttonshape);
    }
}
